import threading
balance = 0
lock = threading.Lock()

def change_it(n):
    global balance
    balance = balance + n
    balance = balance - n

def run_thread(n):
    for i in range(10):
    # 当某个线程开始执行change_it()时，我们说，该线程因为获得了锁，
    # 因此其他线程不能同时执行change_it()，
    # 只能等待，直到锁被释放后，获得该锁以后才能改:
        # lock.acquire()
        # try:
        #     change_it(n)
        # finally:  
        #     lock.release()
        # 会自动锁定与解锁
        with lock:
            change_it(n)
t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)